package com.five.admin.controller;

import com.five.controller.BaseController;
import com.five.admin.service.UserService;
import com.five.entity.User;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("user")
@Api(tags = "用户业务接口")
@CrossOrigin
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     *
     * @return 返回所有用户信息
     */
    @ResponseBody
    @RequestMapping(value = "userList",method = RequestMethod.GET)
    @ApiOperation("获取用户列表接口")
    public BaseResultVO userList(){
        List<User> users = userService.getAllUser();
        return successResp(users);
    }

    /**
     *
     * @param user 用户对象
     * @return 用户数据
     */
    @ResponseBody
    @RequestMapping(value = "selectUser",method = RequestMethod.POST)
    @ApiOperation("查询用户接口")
    public BaseResultVO selectUser(User user){
        User resultUser = userService.getUser(user);
        return successResp(resultUser);
    }

    /**
     * 通过用户id删除用户信息
     * @param user 用户id
     * @return 提示信息
     */
    @ResponseBody
    @RequestMapping(value = "deleteUser",method = RequestMethod.GET)
    @ApiOperation("删除用户接口")
    public BaseResultVO deleteUser(User user){
        userService.deleteUser(user);
        return successRespWithMessage("删除成功");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    @ApiOperation("修改用户信息接口")
    public BaseResultVO updateUser(User user){
        userService.updateUser(user);
        return  successRespWithMessage("修改成功!!");
    }

    @ResponseBody
    @RequestMapping(value = "insertUser",method = RequestMethod.POST)
    @ApiOperation("新增用户信息接口")
    public BaseResultVO inserUser(User user){
        userService.insertUser(user);
        return  successRespWithMessage("成功添加用户信息");
    }

    @ResponseBody
    @RequestMapping(value = "fuzzyQueryUser",method = RequestMethod.POST)
    @ApiOperation("用户信息模糊查询接口")
    public BaseResultVO fuzzyQueryUser(String nickname){
        List<User> users = userService.fuzzyQueryByNickName(nickname);
        return  successResp(users);
    }

    /**
     * 根据电话和密码查询管理员
     * @param user
     * @return 提示信息
     */
    @ResponseBody
    @RequestMapping(value = "toLogin",method = RequestMethod.POST)
    @ApiOperation("管理员登录接口")
    public BaseResultVO toLogin(@ApiParam("登录查询用户对象") User user, HttpServletResponse response) throws IOException {
        if (user == null){
            return errorResp("用户名或密码不能为空");
        }
        User userAdmin = userService.selectAdmin(user.getTelephone());
        System.out.println(userAdmin);
        System.out.println(user.getTelephone());

        if(userAdmin == null){
            return errorResp("用户不存在");
        }
        if (!userAdmin.getPassword().equals(user.getPassword())){
            return errorResp("密码有误");
        }
        if (userAdmin.getType() != 99){
            return errorResp("非管理员不可登录");
        }
        return successResp(userAdmin);
    }

}
